package com.jyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyh.common.R;
import com.jyh.dto.SetmealDto;
import com.jyh.pojo.Category;
import com.jyh.pojo.Setmeal;
import com.jyh.service.CategoryService;
import com.jyh.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     * @param
     * @return
     */
    @PostMapping
    public R<String> addSetmealDish(@RequestBody SetmealDto setmealDto){
        log.info("setmeal:{}",setmealDto.toString());
        log.info("categoryId:{}",setmealDto.getCategoryId());

        setmealService.createSetmealDish(setmealDto);
        return R.success("success");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //log.info("page:{},pageSize:{}",page,pageSize);

        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        //name不为空，按更新时间降序排列
        lqw.like(name!=null,Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage,lqw);
        //setmealPage是有值的，将其复制到setmealDtoPage里面
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //获取查询到的分页数据
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list=new ArrayList<>();

        //遍历分页数据，对每一条数据进行查询
        for(Setmeal record : records ) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            //将菜品名称赋值到disDTO中的category属性
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //健壮性
            if(category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            list.add(setmealDto);
        }
        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }


    /**
     * 编辑页查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> edit(@PathVariable Long id){
        //log.info("id:{}",id);
        SetmealDto setmealDish = setmealService.getSetmealDish(id);

        return R.success(setmealDish);
    }


    /**
     * 更新数据
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //log.info("json;{}",setmealDto.toString());
        //log.info("setmealId;{}",setmealDto.getId());
        setmealService.updateSetmealDish(setmealDto);
        return R.success("success");
    }


    /**
     * 更改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status,@RequestParam List<Long> ids){
        //log.info("status:{}",status);
        //log.info("id:{}",ids);

        List<Setmeal> list = setmealService.listByIds(ids);
        for (Setmeal l :list ) {
            l.setStatus(status);
        }
        setmealService.updateBatchById(list);

        return R.success("success");
    }

    /**
     * 删除、批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        //log.info("ids:");
        setmealService.deleteSetmealDish(ids);
        return R.success("success");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
//        log.info("categoryId:{}",dish.getCategoryId());

        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //根据菜品id
        dishLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());

        //排序
        dishLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //只查起售
        dishLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmeals = setmealService.list(dishLambdaQueryWrapper);

        return R.success(setmeals);

    }
}
